/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewOrganizationAdvSys.java
 * 模块说明：	
 * 修改历史：
 * 2006-4-29 - lxm - 创建。
 */
package com.hd123.rumba.action.organization;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.action.domain.ViewDomainAdvSys;
import com.hd123.rumba.action.user.ViewRoleAdvSys;
import com.hd123.rumba.action.user.ViewUserAdvSys;
import com.hd123.rumba.cartridge.FVRumbaAdvSys;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.organization.IOrganization;
import com.hd123.rumba.organization.biz.BOrganization;
import com.hd123.rumba.organization.biz.IBOrganizationMgr;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.sql.biz.BSQLSubquery;
import com.hd123.rumba.user.IUser;

/**
 * 组织 | Action | FVRumbaAdvSys
 * 
 * @author lxm
 * 
 */
public class ViewOrganizationAdvSys extends BaseViewOrganization implements Action {

  public static final String CAPTION_VIEW = "组织维护(高级)";

  /* 权限常量 */
  public static final String FUNCV_PREFIX = FVRumbaAdvSys.FUNCV_PREFIX;
  public static final String PERMISSION_CREATE = FUNCV_PREFIX + ".组织.新增权";
  public static final String PERMISSION_VIEW = FUNCV_PREFIX + ".组织.查看权";
  public static final String PERMISSION_EDIT = FUNCV_PREFIX + ".组织.编辑权";
  public static final String PERMISSION_DELETE = FUNCV_PREFIX + ".组织.删除权";
  public static final String PERMISSION_RECYCLE_VIEW = FUNCV_PREFIX + ".组织.回收站.查看权";
  public static final String PERMISSION_RECYCLE_EDIT = FUNCV_PREFIX + ".组织.回收站.编辑权";
  public static final String PERMISSION_RECYCLE_UNDELETE = FUNCV_PREFIX + ".组织.回收站.恢复删除权";

  public String execute() throws Exception {
    String url = ActionConstants.NOTIMPLEMENTED;
    dd.put("viewName", "ViewOrganizationAdvSys");

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "list";
    if ("list".equals(startnode)) {
      String isRecycleStr = (String) dd.getFormValue("isRecycle");
      if (isRecycleStr == null)
        isRecycleStr = "FALSE";
      Boolean isRecycle = "TRUE".equalsIgnoreCase(isRecycleStr) ? Boolean.TRUE : Boolean.FALSE;
      if (isRecycle.booleanValue())
        url = this.executeWithPermission("listRecycle", PERMISSION_RECYCLE_VIEW);
      else
        url = this.executeWithPermission("list", PERMISSION_VIEW);
    } else if ("create".equals(startnode))
      url = this.executeWithPermission("create", PERMISSION_CREATE);
    else if ("view".equals(startnode))
      url = this.executeWithPermission("view", PERMISSION_VIEW);
    else if ("edit".equals(startnode))
      url = this.executeWithPermission("edit", PERMISSION_EDIT);
    else if ("delete".equals(startnode))
      url = this.executeWithPermission("delete", PERMISSION_DELETE);
    else if ("undelete".equals(startnode))
      url = this.executeWithPermission("undelete", PERMISSION_RECYCLE_UNDELETE);
    else if ("browse".equals(startnode))
      url = this.executeWithPermission("browse", null);
    else if ("viewOrgDomain".equals(startnode))
      url = this.executeWithPermission("viewOrgDomain", ViewDomainAdvSys.PERMISSION_VIEW);
    else if ("viewUsers".equals(startnode))
      url = this.executeWithPermission("viewUsers", ViewUserAdvSys.PERMISSION_VIEW);
    else if ("viewRoles".equals(startnode))
      url = this.executeWithPermission("viewRoles", ViewRoleAdvSys.PERMISSION_VIEW);
    else if ("dispatch".equals(startnode)) {
      String action = (String) dd.getFormValue("action");
      if (action == null)
        action = "filter";
      if ("list".equals(action))
        url = this.executeWithPermission("list", PERMISSION_VIEW);
      else if ("filter".equals(action))
        url = this.executeWithPermission("filter", PERMISSION_VIEW);
      else if ("save".equals(action)) {
        String isRecycle = (String) dd.getFormValue("isRecycle");
        if ("true".equalsIgnoreCase(isRecycle)) {
          String uuid = (String) dd.getFormValue("org_uuid");
          if ("".equals(uuid))
            uuid = null;
          if (uuid == null)
            url = this.executeWithPermission("save", PERMISSION_CREATE);
          else
            url = this.executeWithPermission("save", PERMISSION_EDIT);
        } else
          url = this.executeWithPermission("save", PERMISSION_RECYCLE_EDIT);
      }
    }

    return url;
  }

  public String getPermission(String functionName, String orgUuid, Boolean isRecycle) {
    return this.getPermission(functionName, IBOrganizationMgr.VALUE_RELATIONSHIP_OTHER, isRecycle);
  }

  public String getPermission(String functionName, int relationship, Boolean isRecycle) {
    if (!isRecycle.booleanValue()) {
      if (ViewOrganization.FUNCTION_CREATE.equals(functionName))
        return PERMISSION_CREATE;
      if (ViewOrganization.FUNCTION_VIEW.equals(functionName))
        return PERMISSION_VIEW;
      if (ViewOrganization.FUNCTION_EDIT.equals(functionName))
        return PERMISSION_EDIT;
      if (ViewOrganization.FUNCTION_DELETE.equals(functionName))
        return PERMISSION_DELETE;
    } else {
      if (ViewOrganization.FUNCTION_VIEW.equals(functionName))
        return PERMISSION_RECYCLE_VIEW;
      if (ViewOrganization.FUNCTION_EDIT.equals(functionName))
        return PERMISSION_RECYCLE_EDIT;
      if (ViewOrganization.FUNCTION_UNDELETE.equals(functionName))
        return PERMISSION_RECYCLE_UNDELETE;
    }
    return null;
  }

  // protected void putFormPermissions(Boolean isRecycle) throws
  // BusinessException {
  // if (isRecycle.booleanValue()) {
  // dd.put("permissionView", PERMISSION_RECYCLE_VIEW);
  // dd.put("permissionEdit", PERMISSION_RECYCLE_EDIT);
  // dd.put("permissionAnotherView", PERMISSION_VIEW);
  // } else {
  // dd.put("permissionView", PERMISSION_VIEW);
  // dd.put("permissionEdit", PERMISSION_EDIT);
  // dd.put("permissionAnotherView", PERMISSION_RECYCLE_VIEW);
  // }
  // dd.put("permissionCreate", PERMISSION_CREATE);
  // dd.put("permissionDelete", PERMISSION_DELETE);
  // dd.put("permissionUndelete", PERMISSION_RECYCLE_UNDELETE);
  // }

  protected void addForceFilter(BSQLSubquery bsq) {
    // Do Nothing
  }

  protected boolean checkForceFilter(BOrganization borg, IUser currentUser, IOrganization currentOrg)
      throws Exception {
    return true;
  }

  protected int getListRelationship(OrganizationFilter filter) throws BusinessException,
      HibernateException {
    return IBOrganizationMgr.VALUE_RELATIONSHIP_OTHER;
  }

}
